﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hcsis.Tools.Windows.EarnBurn
{
    public partial class BindingListMultiFilterControl<T> : UserControl
    {
        public BindingListMultiFilterControl(BindingList<T> list)
        {
            InitializeComponent();
            this.List = list;

        }

        private void FilterList(bool usingFilterText)
        {
            if (usingFilterText)
            {
                //string propName = this.PropertyComboBoxColumn.Text;
                //string val = this.filterToolStripTextBox.Text;
                //if (!string.IsNullOrEmpty(propName))
                //{
                //    this._filteredList = Deloitte.Generics<T>.ToBindingList((from item in this._list where ((string)item.GetType().GetProperty(propName).GetValue(item, null)).ToUpper().IndexOf(val.ToUpper()) > -1 select item).ToList());

                //}
            }
            else
            {
                this._filteredList = this.List;
            }
            if (ListFilteredEvent != null) this.ListFilteredEvent(this, new ListFilteredEventArgs(this._filteredList));
        }

        BindingList<T> _list;

        public BindingList<T> List
        {
            get { return _list; }
            set
            {
                _list = value;
                SetPropertyDropDown(_list);
            }
        }

        private void SetPropertyDropDown(BindingList<T> list)
        {
            this.PropertyComboBoxColumn.Items.Clear();
            if (list != null)
            {
                if (list.Count > 0)
                {
                    Deloitte.Reflection.AddPropertiesToDataGridViewComboBox(list[0].GetType(), PropertyComboBoxColumn);
                }
            }
        }

        BindingList<T> _filteredList = null;
        public event ListFilteredEventHandler ListFilteredEvent;

        protected virtual void OnListFiltered(ListFilteredEventArgs e)
        {
            if (ListFilteredEvent != null)
            {
                ListFilteredEvent(this, e);
            }
        }

        public delegate void ListFilteredEventHandler(object sender, ListFilteredEventArgs e);
        public class ListFilteredEventArgs : EventArgs
        {
            public ListFilteredEventArgs(BindingList<T> list)
            {
                this._filteredList = list;
            }
            private BindingList<T> _filteredList;

            public BindingList<T> FilteredList
            {
                get { return _filteredList; }
            }
        }

    }
}
